package ru.ivanovak.aptu.ccheckers;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {

	private Field gameField;
	private final GameThread gameThread =  new GameThread();
	private class GameThread extends Thread {
		
		private boolean running = false;
		
		private void setRunning(boolean run) {
			running = run;
		}
		
		@Override
		public void run() {
			while (running) {
				
				Canvas canvas = null;
				try {
					canvas = GameView.this.getHolder().lockCanvas();
					synchronized (GameView.this.getHolder()) {
						onDraw(canvas);
					}
				} catch (Exception e) {
					System.err.println("Canvas onDraw exception");
				} finally {
					if (canvas != null) {
						GameView.this.getHolder().unlockCanvasAndPost(canvas);
					}
				}
				synchronized (this) {
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	private void initGameData() {
		final Bitmap coinBmp = 
				BitmapFactory.decodeResource(getResources(), R.drawable.coin);		
		final Bitmap selectedCoinBmp = 
				BitmapFactory.decodeResource(getResources(), R.drawable.selected_coin);		

		gameField = new Field(this, coinBmp, selectedCoinBmp);
		
	}
	
	public GameView(Context context) {
		super(context);
		initGameData();
		
		getHolder().addCallback(new SurfaceHolder.Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				gameThread.setRunning(false);
				boolean retry = true;
				while (retry) {
					try {
						gameThread.join();
						retry = false;
					} catch (InterruptedException e) {
						System.err.println("Interrupted when join");
					}
				}
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				gameThread.setRunning(true);
				gameThread.start();
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) { }
		});
	}
	
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		gameField.onDraw(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		int j = getCellj(e.getX());
		int i = getCelli( e.getY());

		if (e.getAction() == MotionEvent.ACTION_DOWN) {
			gameField.poke(i, j);
			synchronized (gameThread) {
				gameThread.notify();
			}
		}
		
		return true;
	}
	
	private int getCellj(float xt) {
		for (int i = 0, c = 0; i < 1000; i += Coin.WIDTH, ++c) {
			if (xt > i && xt < Coin.WIDTH + i)
				return c;
		}
		return -1;
	}
	
	private int getCelli(float yt) {
		for (int i = 0, c = 0; i < 1000; i += Coin.HEIGHT, ++c) {
			if (yt > i && yt < Coin.HEIGHT+ i)
				return c;
		}
		return -1;
	}

}
